﻿//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Expenses.Controls
{
    public sealed partial class SnappedDatePicker : UserControl
    {
        int thisYear = DateTime.Today.Year;
        int lastYear = DateTime.Today.Year - 1;
        int thisMonth = DateTime.Today.Month;
        int thisDay = DateTime.Today.Day;
        int selectedDay = 0;
        public event EventHandler DateChanged;

        public static DependencyProperty DateProperty
        {
            get
            {
                return
                    DependencyProperty.Register("Date", typeof(DateTime),
                    typeof(DatePicker), new PropertyMetadata(DateTime.Today));
            }
        }

        private DateTime date;
        public DateTime Date
        {
            get { return date; }
            set
            {
                date = value;
                SetDate(value);
            }
        }

        public SnappedDatePicker()
        {
            this.InitializeComponent();

            // Add all 12 months to the Month combo box
            DateTime tempDate;
            for (int i = 1; i < 13; i++)
            {
                tempDate = new DateTime(thisYear, i, 1);
                MonthComboBox.Items.Add(tempDate.ToString("MMM"));
            }
            // The control sets the date to today by default.
            SetDate(DateTime.Today);
        }

        // This can be called by the client.
        public void SetDate(DateTime date)
        {
            // Add date's year and previous year to the Year combo box
            YearComboBox.Items.Add(date.Year.ToString());
            YearComboBox.Items.Add((date.Year - 1).ToString());
            // Default to date's year
            YearComboBox.SelectedValue = date.Year.ToString();

            // Default to date's month
            MonthComboBox.SelectedIndex = date.Month - 1;

            PopulateDayComboBox(date.Month);
            // Default to date's day
            DayComboBox.SelectedIndex = date.Day - 1;
            selectedDay = DayComboBox.SelectedIndex;
        }

        private void PopulateDayComboBox(int month)
        {
            if (DayComboBox.SelectedIndex != -1)
            {
                selectedDay = DayComboBox.SelectedIndex;
            }
            DayComboBox.Items.Clear();
            DateTime tempDate;
            int daysInMonth = 31;

            if (month == 4 || month == 6 || month == 9 || month == 11)
            {
                daysInMonth = 30;
            }
            else if (month == 2)
            {
                if (DateTime.IsLeapYear(thisYear))
                    daysInMonth = 29;
                else
                    daysInMonth = 28;
            }
            for (int i = 1; i <= daysInMonth; i++)
            {
                tempDate = new DateTime(thisYear, month, i);
                if (i < 10)
                {
                    DayComboBox.Items.Add(string.Format("0{0} {1}",
                        i, tempDate.ToString("ddd")));
                }
                else
                {
                    DayComboBox.Items.Add(string.Format("{0} {1}",
                        i, tempDate.ToString("ddd")));
                }
            }
            DayComboBox.SelectedIndex = selectedDay;
        }

        private void YearComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (MonthComboBox.SelectedIndex > 0 && DayComboBox.SelectedIndex > 0)
            {
                PopulateDayComboBox(MonthComboBox.SelectedIndex + 1);
                date = new DateTime(Convert.ToInt32(YearComboBox.SelectedValue),
                    MonthComboBox.SelectedIndex + 1, DayComboBox.SelectedIndex + 1);
                if (DateChanged != null)
                    DateChanged(this, EventArgs.Empty);
            }
        }

        private void MonthComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            PopulateDayComboBox(MonthComboBox.SelectedIndex + 1);
            selectedDay = 0;
            DayComboBox.SelectedIndex = 0;
            date = new DateTime(Convert.ToInt32(YearComboBox.SelectedValue),
                MonthComboBox.SelectedIndex + 1, DayComboBox.SelectedIndex + 1);
            if (DateChanged != null)
                DateChanged(this, EventArgs.Empty);
        }

        private void DayComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (DayComboBox.Items.Count() > 0)
            {
                selectedDay = DayComboBox.SelectedIndex;
            }
            if (MonthComboBox.SelectedItem != null && DayComboBox.SelectedItem != null)
            {
                date = new DateTime(Convert.ToInt32(YearComboBox.SelectedValue),
                    MonthComboBox.SelectedIndex + 1, DayComboBox.SelectedIndex + 1);
                if (DateChanged != null)
                    DateChanged(this, EventArgs.Empty);
            }
        }

    }
}
